/*********************************************************************************
 * MIT License
 *
 * Copyright (c) 2022 jiqila
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 **********************************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void printError();

int main(int argc, char** argv)
{
    if (argc > 1 && argc % 2  == 1)
    {
        char *name, *icon, *exec, *title;
        int count = (argc - 1) / 2;
        for (int i = 0; i < count; i++)
        {
            if (strcmp(argv[i * 2 + 1], "-name") == 0)
                name = argv[i * 2 + 2];
            else if (strcmp(argv[i * 2 + 1], "-icon") == 0)
                icon = argv[i * 2 + 2];
            else if (strcmp(argv[i * 2 + 1], "-exec") == 0)
                exec = argv[i * 2 + 2];
            else if (strcmp(argv[i * 2 + 1], "-title") == 0)
                title = argv[i * 2 + 2];
            else
            {
                printError();
                return -1;
            }
        }

        if (strlen(name) == 0 || strlen(icon) == 0 || strlen(title) == 0 || strlen(exec) == 0)
        {
            printError();
            return -1;
        }

        // 新建快捷方式文件
        char *filename = (char*)malloc(strlen(name) + 33);
        strcat(filename, "/usr/share/applications/");
        strcat(filename, name);
        strcat(filename, ".desktop");

        FILE *file = fopen(filename, "r");
        if (file)
        {
            fclose(file);
            printf("Shortcut file already exist. Please change your file name.\n");
            return -1;
        }

        file = fopen(filename, "w");
        if (file)
        {
            fprintf(file, "[Desktop Entry]\nName=%s\nExec=%s\nTerminal=false\nType=Application\nIcon=%s\nStartupWMClass=%s\nCategories=Development", title, exec, icon, title);
            fclose(file);
            printf("Shortcut created successfully.\n");
        }
        else
        {
            printf("Shortcut creation failed.\n");
            return -1;
        }
    }
    else
    {
        printError();
        return -1;
    }

    return 0;
}

void printError()
{
    printf("-name [shortcut file name]\n");
    printf("-icon [shortcut icon]\n");
    printf("-title [shortcut title]\n");
    printf("-exec [shortcut execute file path]\n");
}